<cfset generator_location = "#rereplace(rereplace(rereplace(cgi.SCRIPT_NAME,'/','.','ALL'),'.','','one'),'.RunGenerator.cfm','', 'ALL')#">
<cfset generator_location = replacenocase(generator_location,"railo.","","ALL") />

<cfsetting showdebugoutput="false" />
<cftry>
	<!--- generate directories --->
	<cfset dir_writer = createObject("component", "DirectoryGenerator").init() />
	<cfset dir_writer.createContext(form.context_name) />
	<cfset dir_writer.createBaseDir() />
	<cfset dir_writer.createModelDir() />
	<cfset dir_writer.createViewDir(form.table_name) />
	<cfset dir_writer.createControllerDir() />
	<cfset dir_writer.createDAODir() />
	<cfset dir_writer.createMigrationsDir() />
	<cfset dir_writer.createPublicDir() />
	<cfset dir_writer.createJSDir() />
	<cfset dir_writer.createImagesDir() />
	<cfset dir_writer.createCSSDir() />
	<cfset dir_writer.createUtilsDir() />
	<cfset dir_writer.createLayout() />

	<cfset file_writer = createObject("component", "#generator_location#.FileGenerator").init() />
	<cfset file_writer.setContext(form.context_name) />
	
	
	<!--- Application, Config, Index files --->
	<cfset file_writer.writeApplicationFiles() />
	
	<cfset file_writer.writeHelper() />
	
	<cfif form.create_controller>
		<!--- write controller --->
		<cfset file_writer.writeController() />
	</cfif>
	
	<cfif form.create_model>
		<!--- write modelfactory file --->
		<cfset file_writer.writeModel() />
	</cfif>
	
	
	<cfset file_writer.writeModelFactory() />
	
	
	<cfif form.create_views>
	<!--- Write View Files --->
		<cfset file_writer.writeView() />
	</cfif>
	
	<cfif form.create_DAO>
		<!--- write dao file --->
		<cfif comparenocase(form.db_name,"sqlite") neq 0>
			<cfset file_writer.writeDAOFile(form.table_name) />
		<cfelse>
			<!--- sqlite template generates sql statements without cfqueryparam --->
			<cfset file_writer.writeDAOFile(form.table_name,"templates/dao_sqlite_template.cfm") />
		</cfif>
	</cfif>
	
	<cfif form.create_migrations>
		<!--- write migrations --->
		<cfset file_writer.writeMigrations() />
	</cfif>
	<cfif form.pretty_urls>
	<cfset file_writer.writehtaccessFile() />
	</cfif>
	<cfset file_writer.writeEclipseProject() />
		<cfoutput>[
		{"result":"success",
		"detail":"New application was generated successfully",
		"message":"Please refer to the stacktrace to verify that all files and folders were generated properly",
		"stacktrace":#serializeJSON(file_writer.WorkLog)#}]</cfoutput>
<cfcatch type="any">
	<cfoutput>[{"result":"failure","detail":"#jsStringFormat(cfcatch.detail)#","message":"#jsStringFormat(cfcatch.Message)#","stacktrace":"#jsStringFormat(cfcatch.stacktrace)#"}]</cfoutput>
	
	<cfset m = createObject("component","cfturbine.gears.Monitor").init() />
	<cfset m.WriteLogToFile(ExpandPath("/#application.env.context#") & "/logs/error-log.html", cfcatch, true) />
</cfcatch>
</cftry>